home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // IdleDemo.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include "Resource.h"
- #include "IdleDemo.h"
-
- CMyApp myApp;
-
- /////////////////////////////////////////////////////////////////////////
- // CMyApp member functions
-
- BOOL CMyApp::InitInstance ()
- {
- m_pMainWnd = new CMainWindow;
- m_pMainWnd->ShowWindow (m_nCmdShow);
- m_pMainWnd->UpdateWindow ();
- return TRUE;
- }
-
- BOOL CMyApp::OnIdle (LONG lCount)
- {
- CWinApp::OnIdle (lCount);
-
- BOOL bReturn = TRUE;
- if (lCount >= 2) {
- CMainWindow* pWnd = (CMainWindow*) m_pMainWnd;
- if (!pWnd->CursorInClient ()) {
- pWnd->UpdateReadout ();
- bReturn = FALSE;
- }
- }
- return bReturn;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMainWindow message map and member functions
-
- BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
- ON_WM_PAINT ()
- ON_WM_MOUSEMOVE ()
- ON_COMMAND (IDM_EXIT, OnExit)
- END_MESSAGE_MAP ()
-
- CMainWindow::CMainWindow ()
- {
- Create (NULL, "IdleDemo", WS_OVERLAPPEDWINDOW, rectDefault,
- NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
- }
-
- void CMainWindow::OnPaint ()
- {
- CPaintDC dc (this);
- UpdateReadout (&dc);
- }
-
- void CMainWindow::OnMouseMove (UINT nFlags, CPoint point)
- {
- UpdateReadout ();
- }
-
- void CMainWindow::OnExit ()
- {
- SendMessage (WM_CLOSE, 0, 0);
- }
-
- BOOL CMainWindow::CursorInClient ()
- {
- CRect rect;
- GetClientRect (&rect);
- ClientToScreen (&rect);
-
- POINT point;
- ::GetCursorPos (&point);
-
- BOOL bReturn = FALSE;
- if (rect.PtInRect (point) && (WindowFromPoint (point) == this))
- bReturn = TRUE;
-
- return bReturn;
- }
-
- void CMainWindow::UpdateReadout (CDC* pDC)
- {
- BOOL bDeleteDC = FALSE;
- if (pDC == NULL) {
- pDC = new CClientDC (this);
- bDeleteDC = TRUE;
- }
-
- CRect rect;
- GetClientRect (&rect);
- int nWidth = rect.Width ();
- int nHeight = rect.Height ();
-
- TEXTMETRIC tm;
- pDC->GetTextMetrics (&tm);
- int cx = tm.tmAveCharWidth;
- int cy = tm.tmHeight;
-
- rect.left = (nWidth - (cx * 16)) / 2;
- rect.right = rect.left + (cx * 16);
- rect.top = (nHeight - (cy * 2)) / 2;
- rect.bottom = rect.top + (cy * 2);
-
- pDC->Rectangle (rect);
-
- if (CursorInClient ()) {
- POINT point;
- ::GetCursorPos (&point);
- ScreenToClient (&point);
-
- CString strReadout;
- strReadout.Format ("%d, %d", point.x, point.y);
-
- pDC->DrawText (strReadout, &rect, DT_CENTER | DT_VCENTER |
- DT_SINGLELINE);
- }
-
- if (bDeleteDC)
- delete pDC;
- }
-